#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <crtdbg.h>
#pragma warning(disable : 4996)

char * read(char stop_char)
{
	int i;
	int n=10;
	char* l= (char*)malloc((n+1)*sizeof(char));
	
	
	for(i=0; 1;i++)
	{
		l[i]=fgetc(stdin);
		
		if (l[i] == stop_char)
		{
			l[i] = '\0';
			break;
		}
		if(i==n)
		{
		  n=n+10;
		 l=(char*)realloc(l,(n+1)*sizeof(char));
		}

	}
		
	
	
	return l;
}

int main(int argc, char * argv[])
{
	FILE* file_1;
	FILE* file_2;
	
	file_1=fopen(argv[1],"rb");
	file_2=fopen(argv[2],"wb");
	if(file_1==NULL)
	{
		printf("Error!");
		return 1;
	}
	if(file_2==NULL)
	{
		printf("Error!");
		fclose(file_1);
		return 2;
	}
	
	char * key;
	
	int i = 0, len;
	printf("Enter the password\n");
	
	key = read('\n');


	len = strlen(key);
	while(!feof(file_1))
	{
		char k;
		char c;
		c=getc(file_1);
		k=c^key[i%len];
		/*if(c==EOF)
		{
			break;
		}*/
		
			putc(k,file_2);
		fflush(file_2);
		
		i++;
	}
	
	printf("Decoding finished");
	free(key);
	fclose(file_1);
	fclose(file_2);
	system("pause");

	_CrtDumpMemoryLeaks();

	return 0;
}	